package org.platform.processor.diagnose.job;

import com.github.pagehelper.PageHelper;
import com.mc.mic.core.log.bus.BusLogPusher;
import com.mc.mic.core.log.frame.LogBo;
import lombok.extern.slf4j.Slf4j;
import org.platform.constant.DiagnoseConst;
import org.platform.constant.LogType;
import org.platform.core.JobManager;
import org.platform.dao.mapper.FdDiagnosetaskMapper;
import org.platform.mo.bussmo.log.TaskDiagnoseTaskMqBO;
import org.platform.mo.db.FdDiagnosetask;
import org.quartz.*;
import org.quartz.impl.JobDetailImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.PostConstruct;
import java.util.ArrayList;
import java.util.List;

/**
 * 推送中诊断任务
 *
 * @author FengJie
 * @date 2018/6/19
 */
@Slf4j
@Component
public class PromoteDiagnoseTaskJob implements InterruptableJob {
    private static final String JOB_KEY = "PromoteDiagnoseTaskJob";

    @Autowired
    private FdDiagnosetaskMapper mapper;
    @Autowired
    private BusLogPusher mqLog;
    @Autowired
    private JobManager job;

    @Override
    public void interrupt() {

    }

    @Override
    public void execute(JobExecutionContext context) {
        try {
            mqLog.log(new LogBo<>(LogType.DIAGNOSE_TASK_LOG, new TaskDiagnoseTaskMqBO(getTask())));
        } catch (Exception e) {
            log.error("定时器【{}】发生错误,异常信息", JOB_KEY, e);
        }
    }

    /**
     * 初始化当前定时器
     */
    @PostConstruct
    public void init() {
        JobDetailImpl jd = new JobDetailImpl();
        jd.setJobClass(PromoteDiagnoseTaskJob.class);
        jd.setKey(JobKey.jobKey(JOB_KEY));

        SimpleTrigger trigger = TriggerBuilder.newTrigger()
                .withSchedule(SimpleScheduleBuilder
                        .simpleSchedule()
                        .withIntervalInSeconds(10)
                        .repeatForever())
                .startNow()
                .build();

        job.register(jd, trigger);
    }

    /**
     * 获取客户端所需的任务信息
     * 规则:总计5条数据
     * (正在执行的1个   三个待执行的3个  1个最近执行完的  共计5个  )
     *
     * @return
     */
    private List<FdDiagnosetask> getTask() {
        final int count = 5;
        final int execute = 1;
        final int wait = 3;
        final int success = 1;
        List<FdDiagnosetask> taskList = new ArrayList<>(count);

        PageHelper.offsetPage(0, execute);
        Example e1 = new Example(FdDiagnosetask.class);
        e1.createCriteria().andEqualTo("status", DiagnoseConst.DIAGNOSE_STATUS_EXECUTE);
        List<FdDiagnosetask> executeList = mapper.selectByExample(e1);

        PageHelper.offsetPage(0, count - executeList.size() - success);
        Example e2 = new Example(FdDiagnosetask.class);
        e2.createCriteria().andEqualTo("status", DiagnoseConst.DIAGNOSE_STATUS_WAIT);
        e2.orderBy("starttime").desc();
        List<FdDiagnosetask> waitList = mapper.selectByExample(e2);

        PageHelper.offsetPage(0, success);
        Example e3 = new Example(FdDiagnosetask.class);
        e3.createCriteria().andEqualTo("status", DiagnoseConst.DIAGNOSE_STATUS_SUCCESS);
        e3.orderBy("endtime").desc();
        List<FdDiagnosetask> successList = mapper.selectByExample(e3);

        taskList.addAll(executeList);
        taskList.addAll(waitList);
        taskList.addAll(successList);
        return taskList;
    }
}
